package com.hmall.fileuploadproject.controller;

import com.hmall.fileuploadproject.utils.AliyunOSSUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/upload")
@CrossOrigin(origins = "*")
public class uploadController {
    @PostMapping("/file")
    public String uploadFile(String username, String  password, MultipartFile file) throws IOException{
        System.out.println( "----------------------"+username);
        String originalFilename = file.getOriginalFilename();
        //设置文件名称
        String fileName = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
        file.transferTo(new File("D:/idea/"+fileName));
        return fileName;
    }
    @PostMapping("/fileOss")
    public String uploadFilePost(MultipartFile file) throws Exception {
        String originalFilename = file.getOriginalFilename();
//        //后缀名称
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
//        file.transferTo(new File("D:/"+fileName));
        String fileName =AliyunOSSUtils.upload("https://oss-cn-guangzhou.aliyuncs.com", "java-upload-test", file.getBytes(), extName);
        return fileName;
    }
}
